﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BarrageGameAssistant.UIForms
{
    public partial class FrmSelectProcess : Form
    {
        public string? ProcessName { get; set; }

        public FrmSelectProcess()
        {
            InitializeComponent();
        }

        private void FrmSelectProcess_Load(object sender, EventArgs e)
        {
            RefreshProcess();
        }

        private void RefreshProcess()
        {
            var Processes = Process.GetProcesses();
            foreach (Process p in Processes)
            {
                if (string.IsNullOrEmpty(p.MainWindowTitle))
                    continue;
                if (p.MainModule != null)
                {
                    var item = lvProcess.Items.Add(p.Id.ToString());
                    item.Tag = p.ProcessName;
                    item.SubItems.Add(p.ProcessName);
                    item.SubItems.Add(p.MainWindowTitle);
                    item.SubItems.Add(p.MainModule?.FileName);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            var items = lvProcess.SelectedItems;
            if (items != null && items.Count > 0)
            {
#pragma warning disable CS8602 // 解引用可能出现空引用。
                this.ProcessName = items[0].Tag.ToString();
#pragma warning restore CS8602 // 解引用可能出现空引用。

                this.DialogResult = DialogResult.OK;
            }


        }
    }
}
